package com.kotlin.demo.ui

import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.*
import androidx.compose.material.OutlinedButton
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.kotlin.demo.bean.ResBean
import com.kotlin.demo.constant.Constants
import com.kotlin.demo.helper.DialogHelper
import com.kotlin.demo.tools.FileUtils
import com.kotlin.demo.tools.ResPathUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

/**
 * 添加翻译文件写入路径 弹窗
 */
@Composable
@Preview
internal fun addResPath() {
    var resPathContent by remember { mutableStateOf("") }//新增路径缓存
    var resPathTag by remember { mutableStateOf("") }//新增路径缓存 key
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier.padding(start = 18.dp, end = 18.dp, top = 18.dp, bottom = 10.dp),
    ) {
        Text(
            text = "新增翻译路径", style = TextStyle(fontSize = 14.sp), modifier = Modifier.padding(bottom = 10.dp)
        )
        Row {
            OutlinedTextField(
                value = resPathTag,
                onValueChange = { resPathTag = it },
                label = { Text("请输入翻译路径简称", style = TextStyle(fontSize = 14.sp)) },
                textStyle = TextStyle(
                    fontSize = 14.sp, color = Color.Black
                ),
                modifier = Modifier.width(280.dp).padding(0.dp)// 使 TextField 填满其父组件的宽度
            )
            Spacer(modifier = Modifier.width(16.dp))
            OutlinedTextField(
                singleLine = true,
                value = resPathContent,
                onValueChange = { resPathContent = it },
                label = { Text("请输入翻译写入路径", style = TextStyle(fontSize = 14.sp)) },
                maxLines = 1,
                textStyle = TextStyle(
                    fontSize = 14.sp, color = Color.Black
                ),
                modifier = Modifier.width(280.dp).padding(bottom = 0.dp) // 使 TextField 填满其父组件的宽度
            )
        }
        Spacer(modifier = Modifier.height(16.dp))
        Row {
            OutlinedButton(
                onClick = {
                    if (resPathContent.trim() == ("")) {
                        return@OutlinedButton
                    }
                    if (resPathTag.trim() == ("")) {
                        return@OutlinedButton
                    }
                    DialogHelper.showLoadingDialog("正在添加中...请稍后！")
                    Constants.mainScope.launch(Dispatchers.IO) {
                        val bean = ResPathUtils.getResData()
                        bean.resList.add(ResBean(resPathTag, resPathContent))
                        val json = try {
                            Json.encodeToString(bean)
                        } catch (e: Exception) {
                            println(e)
                            "{}"
                        }
                        FileUtils.stringToFile(json)
                        delay(500L)
                        DialogHelper.dismissDialog()
                        delay(200L)
                        DialogHelper.showTipsDialog("添加成功！")
                    }
                },
                content = { Text("写入", style = TextStyle(fontSize = 14.sp)) },
            )
            OutlinedButton(
                onClick = {
                    DialogHelper.dismissDialog()
                },
                content = { Text("取消", style = TextStyle(fontSize = 14.sp)) },
                modifier = Modifier.padding(start = 8.dp)
            )
        }
        Spacer(modifier = Modifier.height(8.dp))
    }
}
